package registers;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author Kevin
 *
 */
public class RegisterManager
{
	private ArrayList<Register> register_pool;

	/**
	 * Constructor
	 * @param number_of_registers Number of desired registers
	 * @param taxRate The rate at which to Tax
	 */
	public RegisterManager(int number_of_registers, double taxRate)
	{
		register_pool = new ArrayList<Register>();
		for(int i = 0; i < number_of_registers; i++)
		{
			register_pool.add(new Register(i+1));
			register_pool.get(i).setTax(taxRate);
		}
	}

	public int getPoolSize()
	{
		return register_pool.size();
	}

	public ArrayList<Integer> getRegisterNumbers()
	{
		ArrayList<Integer> list = new ArrayList<Integer>();
		for(Register r : register_pool)
		{
			list.add(r.getID());
		}

		return list;
	}
	/**
	 * Find the register with the specified ID number
	 * @param id The requested ID number
	 * @return the register with the corresponding ID number or null if it does not exist
	 */
	private Register getRegByID(int id)
	{
		for(Register r : register_pool)
		{
			if(r.getID() == id)
			{
				return r;
			}
		}
		return null;
	}

	public void printReceipt(data.Order o)
	{
		final String LINE_SIZE = "                                          ";
		StringBuffer sb1 = new StringBuffer();
		StringBuffer sb2 = new StringBuffer();
		double price1 = o.getSubtotal();
		double price2 = register_pool.get(0).taxCalc(o.getSubtotal());

		String pre1 = "Subtotal";
		String post1 = (price1>0?formatPrice(price1):"");
		String middle1 = LINE_SIZE.substring(pre1.length() + post1.length());

		String pre2 = "Total";
		String post2 = (price2>0?formatPrice(price2):"");
		String middle2 = LINE_SIZE.substring(pre2.length() + post2.length());

		sb1.append(pre1).append(middle1).append(post1);
		sb2.append(pre2).append(middle2).append(post2).append("\n");

		System.out.println(o.toString());
		System.out.println(sb1.toString());
		System.out.println(sb2.toString());
	}
	/**
	 * Takes and order and asks the user what the customer paid, then
	 * credits the appropriate register
	 * @param o The specific order being rung up
	 */
	public void handleMoney(data.Order o)
	{
		Register r = getRegByID(o.getRegister());
		if(r == null)
		{
			System.out.println("Register number does not exist in the system.\n");
			return;
		}
		double amount = o.getSubtotal();
		Scanner in = new Scanner(System.in);
		double paid = 0.00;
		boolean finished = false;
		double change = 0.00;

		while(!finished)
		{
			System.out.printf("The amount due is " + formatPrice(r.taxCalc(amount)) + ", how much did the customer pay?: $ ");
			paid = in.nextDouble();
			if((change = r.creditRegister(amount, paid)) < 0)
			{
				System.out.println("\nThe amount paid is below the " + formatPrice(amount) + " required. Please try again.\n");
			}
			else
			{
				finished = true;
			}
		}
		System.out.println("Present " + formatPrice(change) + " in change to the customer.\n");
	}

	/**
	 * @param p double price value 
	 * @return currency format double
	 */
	private static String formatPrice(double p){
		return NumberFormat.getCurrencyInstance().format(p);
	}

	public static void main(String args[])
	{
//		RegisterManager r = new RegisterManager(2, 0.07);
//		data.Order o = new data.Order(1, new factory.OrderObject("ICE CREAM", 2.25, true));
//
//		r.handleMoney(o);
	}


}
